//package com.xb.system.service.impl;
//
//import com.xb.model.domain.Users;
//import com.xb.system.service.DynamicDataSplitterService;
//import com.xb.system.service.FileHandler;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import java.io.BufferedReader;
//import java.io.FileReader;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.TimeUnit;
//
//@Slf4j
////@Component
//public class TxtFileHandler2 implements FileHandler {
//
//    @Value("${file.batch.size}")
//    private int batchSize;
//
//    @Value("${file.thread.pool.size}")
//    private int threadPoolSize;
//
//    @Autowired
//    private DynamicDataSplitterService dynamicDataSplitterService;
//
//    @Override
//    public void handleFile(String file) throws IOException {
//        // TODO 配置都抽离到配置文件中
//        ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize); // 设置线程池大小
//        // TODO 采用内存映射技术优化读取性能
////        try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
////             FileChannel channel = randomAccessFile.getChannel()) {
////            long fileSize = channel.size();
////            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
////            // TODO 记录偏移量避免读取错误
////            String line;
////            int lineCount = 0;
////            long currentOffset = 0; // 记录当前的文件偏移量
////            // TODO 采用多线程进行读写并发、并行，异步线程监听大小锁定阈值
////            List<Users> batch = new ArrayList<>();
////            StringBuilder sb = new StringBuilder();  // 用于缓存每行数据的字符串构建器
////            
////            while (buffer.hasRemaining()) {
////                byte[] bytes = new byte[Math.min(buffer.remaining(), 4096)]; // 4096 是举例用的缓冲区大小，可以根据实际情况调整
////                buffer.get(bytes);
////                String chunk = new String(bytes, StandardCharsets.UTF_8);
////                sb.append(chunk);  // 将读取的数据追加到 StringBuilder 中
////
////                // 查找换行符来切分行
////                int index;
////                while ((index = sb.indexOf("\n")) != -1) {
////                    line = sb.substring(0, index).trim(); // 提取当前行并去除首尾空白
////                    sb.delete(0, index + 1); // 删除已处理的行和换行符
////
////                    // 处理当前行
////                    lineCount++;
////                    splitterService.processLine(line, batch);
////
////                    if (batch.size() >= batchSize) {
////                        List<Users> batchToSave = new ArrayList<>(batch);
////                        // 分割批处理数据并提交给线程池
////                        processingSmallData(executor, batchToSave);
////                        batch.clear();
////                    }
////                }
////            }
//
//        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
//            String line;
//            List<Users> batch = new ArrayList<>();
//
//
//            while ((line = reader.readLine()) != null) {
//
//                dynamicDataSplitterService.processLine(line.trim(), batch);
//
//                if (batch.size() >= batchSize) {
//                    List<Users> batchToSave = new ArrayList<>(batch);
//                    // 分割批处理数据并提交给线程池
//                    processingSmallData(executor, batchToSave);
//                    batch.clear();
//                    log.info("已处理 {} 行，",  batchToSave.size());
//                }
//            }
//            // 处理最后剩余的数据
//            if (!batch.isEmpty()) {
//                List<Users> batchToSave = new ArrayList<>(batch);
//                processingSmallData(executor, batchToSave);
//                batch.clear();
//            }
//
//        } catch (IOException e) {
//            log.error("读取文件时发生错误：{}", e.getMessage());
//        } finally {
//            executor.shutdown();
//            try {
//                executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
//            } catch (InterruptedException e) {
//                log.error("等待线程池关闭时发生错误：{}", e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
//
//
//    }
//
//    /**
//     * 分割成小份数据再入库
//     * @param executor
//     * @param batchToSave
//     */
//    private void processingSmallData(ExecutorService executor, List<Users> batchToSave) {
//        int subBatchSize = batchSize / 10; // 分成10份
//        for (int i = 0; i < batchToSave.size(); i += subBatchSize) {
//            int end = Math.min(i + subBatchSize, batchToSave.size());
//            List<Users> subBatch = batchToSave.subList(i, end);
//            executor.submit(() -> dynamicDataSplitterService.saveBatch(new ArrayList<>(subBatch)));
//        }
//    }
//}
